//
//  RHDeviceStatusVC.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/12/4.
//

#import "RHDeviceStatusVC.h"
#import "JXCategoryView.h"
#import "JXCategoryListContainerView.h"
#import "RHDeviceSystemVC.h"
#import "RHDeviceInfoListVC.h"
#import "RHDevicePowerVC.h"
#import "RHDeviceElectricEnergyVC.h"
#import "RHDeviceLoadVC.h"
#import "RHNavTitleView.h"

@interface RHDeviceStatusVC ()<JXCategoryListContainerViewDelegate,JXCategoryViewDelegate,JXCategoryTitleViewDataSource>
@property (nonatomic, strong) NSArray *titles;

@property (nonatomic, strong) JXCategoryTitleView *categoryView;

@property (nonatomic, strong) JXCategoryListContainerView *listContainerView;

@property (nonatomic, strong) RHNavTitleView *titleView;
@end

@implementation RHDeviceStatusVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = CFTabViewBgColor;
    [self setupUI];
}

- (void)setupUI{
    self.titles = @[@"系统".rh_localized,@"功率".rh_localized,@"电能".rh_localized,@"电池".rh_localized];
    
    self.titleView = [[RHNavTitleView alloc]init];
    [self.titleView hideArrowImg];
    self.titleView.devNameLab.text = self.deviceModel.deviceName;
    self.titleView.devIdLab.text = self.deviceModel.deviceId;
    self.titleView.status = self.deviceModel.status;
    self.navigationItem.titleView = self.titleView;
    
    UIButton *setButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0,40, 64)];
    [setButton setTitle:@"详情".rh_localized forState:UIControlStateNormal];
    [setButton setTitleColor:kCOLOR_HEX(@"3F4443") forState:UIControlStateNormal];
    [setButton.titleLabel setFont:[UIFont systemFontOfSize:15]];
    
    [setButton addTarget:self action:@selector(deviceDetail) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarBtn = [[UIBarButtonItem alloc] initWithCustomView:setButton];
    self.navigationItem.rightBarButtonItem = rightBarBtn;
    
    _listContainerView = [[JXCategoryListContainerView alloc] initWithType:JXCategoryListContainerType_ScrollView delegate:self];
    [self.view addSubview:self.listContainerView];
    
    _categoryView = [[JXCategoryTitleView alloc] initWithFrame:CGRectMake(0,0, [UIScreen mainScreen].bounds.size.width, 50)];
    self.categoryView.titles = self.titles;
    self.categoryView.backgroundColor = [UIColor whiteColor];
    self.categoryView.delegate = self;
//    self.categoryView.cellWidth = (SCREEN_WIDTH-30)/4;
    self.categoryView.titleSelectedColor = CMainColorGreen;
    self.categoryView.titleColor = kCOLOR_HEX(@"969799");
    self.categoryView.titleFont = [UIFont systemFontOfSize:14];
    self.categoryView.titleSelectedFont = [UIFont boldSystemFontOfSize:14];
    self.categoryView.titleColorGradientEnabled = YES;
    self.categoryView.titleLabelZoomEnabled = NO;
    self.categoryView.titleLabelZoomScale = 1.2;
    self.categoryView.cellSpacing = 15;
    self.categoryView.contentEdgeInsetLeft = 30;
    self.categoryView.contentEdgeInsetRight = 30;
    self.categoryView.titleLabelVerticalOffset = -10;
    [self.view addSubview:self.categoryView];
    self.categoryView.listContainer = self.listContainerView;
    
    JXCategoryIndicatorBackgroundView *backgroundView = [[JXCategoryIndicatorBackgroundView alloc] init];
     backgroundView.size = CGSizeMake(60, 3);
     backgroundView.indicatorColor = CMainColorGreen;
     backgroundView.indicatorHeight = 3;
//     backgroundView.indicatorWidth = 60;
     backgroundView.verticalMargin = -10;
     backgroundView.indicatorCornerRadius = 1.5;

     _categoryView.indicators = @[backgroundView];
    
    
    [_categoryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.mas_offset(0);
        make.height.mas_equalTo(50);
    }];
    
    [_listContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(self.categoryView.mas_bottom).offset(0);
        make.bottom.mas_offset(0);
    }];
    
    kWeakSelf;
    [[setButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {

    }];
}

#pragma mark ------ 获取设备信息(topo数据,有无可升级固件)
-(void)updateDevInfo
{
   
    NSString *url = [NSString stringWithFormat:@"%@%ld",H_getDeviceInfo,self.deviceModel.device_id];
    [BaseModel startGetWithCompletionAPIKey:url argument:@{} requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        if(resultModel.code == 200){
            self.titleView.status = request.responseJSONObject[@"data"][@"status"];
            
        }
        
    } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
            
    }];
}

-(void)deviceDetail
{
    RHDeviceInfoListVC *infoListVC = [[RHDeviceInfoListVC alloc]init];
    infoListVC.deviceModel = self.deviceModel;
    [self.navigationController pushViewController:infoListVC animated:YES];
}


#pragma mark - JXCategoryViewDelegate

// 点击选中或者滚动选中都会调用该方法。适用于只关心选中事件，不关心具体是点击还是滚动选中的。
- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index {
    NSLog(@"%@", NSStringFromSelector(_cmd));
    // 侧滑手势处理
    self.navigationController.interactivePopGestureRecognizer.enabled = (index == 0);

}

// 滚动选中的情况才会调用该方法
- (void)categoryView:(JXCategoryBaseView *)categoryView didScrollSelectedItemAtIndex:(NSInteger)index {
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

#pragma mark - JXCategoryListContainerViewDelegate
// 返回列表的数量
- (NSInteger)numberOfListsInlistContainerView:(JXCategoryListContainerView *)listContainerView {
    return  self.titles.count;
}

// 返回各个列表菜单下的实例，该实例需要遵守并实现 <JXCategoryListContentViewDelegate> 协议
- (id<JXCategoryListContentViewDelegate>)listContainerView:(JXCategoryListContainerView *)listContainerView initListForIndex:(NSInteger)index {
    kWeakSelf;
    if(index ==0){
        //系统
        RHDeviceSystemVC *vc = [[RHDeviceSystemVC alloc]init];
        vc.updateDevInfoBlock = ^(NSString *status,NSString *devName){
            weakSelf.titleView.status = status;
            weakSelf.titleView.devNameLab.text = devName;
        };
        vc.deviceModel = self.deviceModel;
        return vc;
    }else if (index == 1){
        //功率
        RHDevicePowerVC *vc = [[RHDevicePowerVC alloc]init];
        vc.updateDevInfoBlock = ^{
            [weakSelf updateDevInfo];
        };
        vc.devModel = self.deviceModel;
        return vc;
    }else if (index == 2){
        //电能
        RHDeviceElectricEnergyVC *vc = [[RHDeviceElectricEnergyVC alloc]init];
        vc.updateDevInfoBlock = ^{
            [weakSelf updateDevInfo];
        };
        vc.devModel = self.deviceModel;
        return vc;
    }else{
        //电池
        RHDeviceLoadVC *loadVC = [[RHDeviceLoadVC alloc]init];
        loadVC.updateDevInfoBlock = ^{
            [weakSelf updateDevInfo];
        };
        loadVC.deviceId = self.deviceModel.deviceId;
        return loadVC;
    }
    
    return [RHDeviceSystemVC new];
}

@end
